// pages/shop/shop.js
import api from '../http/api'
//全局变量
const app = getApp();
Page({
    /**
     * 页面的初始数据
     */
      data: {
        ill_mes:[],
        isif:false,        //没有订单提示创建
        if_tc:false,
        index:'',
        delet_username:'',
        display:'block',
        refresh_loading:'',
        address_text:'',
        //获取订单状态
        get_status:'',
        warn_login:false,
      },
      //提交订单,仅加载动画
      submit(e){
           //先记录index值
           this.data.index = e.currentTarget.dataset.index
            //保存用户名提供删除/订单状态查询
          this.data.delet_username = this.data.ill_mes[e.currentTarget.dataset.index].username
          this.setData({
              index:e.currentTarget.dataset.index
          })
             //   后执行
          if(this.data.ill_mes[e.currentTarget.dataset.index].status == '已通过'){
             app.globalData.parameter = 1;
              wx.switchTab({
                url: '/pages/order/order',
              })
          }
          if(this.data.ill_mes[e.currentTarget.dataset.index].status == '提交信息'){
            //   先执行
            this.setData({
                refresh_loading:true
               })
               let timer = setInterval(() => {
                   //发生请求更改status
                    api.postData('/update/order_lists?username='+ this.data.delet_username+'&status=审核中').then(res=>{
                            console.log(res);
                        })
                    this.setData({
                        refresh_loading:'',
                    })
                    //刷新
                    this.onShow()
                    clearInterval(timer)
               }, 1500);
          }
      },
      //跳转
      goto_write(){
        if(app.globalData.islogin ==true){
            wx.navigateTo({
            url: '../viewsons/write_mess/write_mess',
            })
         }
      },
      add(){
         //开启弹窗
         if(app.globalData.islogin ==true){
            this.data.display = 'block'
            this.setData({
                display:this.data.display
            })
            this.data.top = "20%"
            this.setData({
                top:this.data.top,
            })
         }else{
            this.setData({
                warn_login:true,
            })
            let timer = setInterval(() => {
                this.setData({
                    warn_login:false,
                })
                clearInterval(timer)
            }, 2000);
         }
      },
      cancel(){
        this.data.top = 100+'%'
        //关闭弹窗
        let timer = setInterval(() => {
            console.log('定时器关闭');
            this.data.display = 'none'
            this.setData({
                display:this.data.display
            })
            clearInterval(timer)
        }, 2000);
        
        this.setData({
            top:this.data.top,
        })
      },
      //预删除
      pre_delet(e){
          //先记录index值
          this.data.index = e.currentTarget.dataset.index
          //保存用户名提供删除查询
          this.data.delet_username = this.data.ill_mes[e.currentTarget.dataset.index].username
          console.log( this.data.delet_username);
          this.setData({
              index:this.data.index,
              delet_username:this.data.delet_username,
              if_tc:true,
          })
      },
      cancel_delet(){
        this.setData({
            if_tc:false,
        })
      },
      //删除
      tapDialogButton(e){
          console.log(e.detail.index);
        if(e.detail.index==0){
             //取消弹窗
             this.setData({
                 if_tc:false
             })
        }else{
            //确定删除
            api.postData('/order_lists/deleted?username='
            +this.data.delet_username
            ).then(res=>{
                console.log(res);
            })
            this.data.ill_mes.splice(this.data.delet_code,1)
            if(this.data.ill_mes.length==0){
                this.data.isif = false
            }
            this.setData({
                ill_mes:this.data.ill_mes,
                isif:this.data.isif,
                if_tc:false,
            })
            //刷新
            this.onShow()
        }
       },
     
      gotoaddress(){
        wx.navigateTo({
          url: '../viewsons/my_address/my_address?selecting=1',
        })
      },
    
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
       this.setData({
           address_num:app.globalData.current_address_num
       })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        if(app.globalData.refresh_shop ==true){
            app.globalData.refresh_shop = false
            this.onShow
        }
        this.setData({
            ill_mes:[]
        })
        //登录判断
    if(app.globalData.islogin ==true){
        api.getData('/order_lists').then(res=>{
            this.setData({
                ill_mes:res.params.data
             })
            //要放在请求里面
               //先把数组打开
         if(this.data.ill_mes.length != 0){
            this.setData({
                isif:true
            })
        }
                //先把数组关掉
                if(this.data.ill_mes.length == 0){
                    this.setData({
                        isif:false
                    })
                }
        })
    }//判断是否登陆
             
    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})